#!/usr/bin/env python3

import sys, getopt
opts, args = getopt.getopt(sys.argv[1:],"hi:o:")
input_file = ""
output_file = ""
for op, value in opts:
    if op == "-i":
        input_file = value
    elif op == "-o":
        output_file = value
    elif op == "-h":
        print("you can use -i to choose input file, \n -o to determin outputfile,\n by default if outfile not defined,will the same with input file.\n For help,please use -h")

if input_file =="":
    print("you shuld use -i to choose input file")
    sys.exit()
if output_file == "":
    output_file = input_file+".h"


def min_tuple(x,y):
    if x[1] >= 0:
        if y[1] >= 0:
            if x[1] < y[1]:
                return x
            else:
                return y
        else:
            return x
    else:
        return y

def next_tag_position(start):
    left_pos = srcCode.find("{",start)
    right_pos = srcCode.find("}",start)
    class_pos = srcCode.find("class",start)
    annotation_pos = srcCode.find("/*",start)
    return min_tuple(min_tuple(("{",left_pos),("}",right_pos)), min_tuple(("class",class_pos),("/*",annotation_pos)))


f = open("BearerData.java")
srcCode = f.read()
f.close()
outCode = srcCode

left_pos_stack = []
return_tuple = next_tag_position(0)
is_class = False
ignore_left = 0

while return_tuple[1] >= 0:
    if return_tuple[0] == "{":
        if is_class == True:
            temp_tuple = ("{class",return_tuple[1])
            return_tuple = temp_tuple
            is_class = False
            left_pos_stack.append(return_tuple)
        else:
            previous_left = left_pos_stack.pop()
            if previous_left[0] == "{":
                left_pos_stack.append(previous_left)
                ignore_left = ignore_left + 1
            else:
                left_pos_stack.append(previous_left)
                left_pos_stack.append(return_tuple)


    elif return_tuple[0] == "}":
        if ignore_left > 0:
            ignore_left = ignore_left - 1
        else:
            previous_left = left_pos_stack.pop()
            if previous_left[0] == "{":
                outCode = outCode.replace(srcCode[previous_left[1]:return_tuple[1]+1],";\n")
    elif return_tuple[0] == "class":
        is_class = True
    elif return_tuple[0] == "/*":
        annotation_end = srcCode.find("*/",return_tuple[1]+1)
        if annotation_end < 0:
            print("annotation is not matched,exit.....")
            sys.exit()
        return_tuple = ("",annotation_end)
    return_tuple = next_tag_position(return_tuple[1]+1)

outfile = open("BearerData.h",'w')
outfile.write(outCode)
outfile.close()

print ("success")




